package cn.zbh.org.controller;

import cn.zbh.basic.util.AjaxResult;
import cn.zbh.basic.util.ExcelUtils;
import cn.zbh.basic.util.PageList;
import cn.zbh.org.domain.Shop;
import cn.zbh.org.dto.ShopDto;
import cn.zbh.org.domain.ShopLog;
import cn.zbh.org.query.ShopQuery;
import cn.zbh.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(value = "店铺的查询",description="店铺相关的CRUD功能")
public class ShopController {

    @Autowired
    private IShopService shopService;
    @GetMapping
    @ApiOperation(value = "通过ID查询所有" )
    public List<Shop> findAll(){
        return shopService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Shop findById(@PathVariable("id") Long id){
        return shopService.findById(id);
    }

    @PostMapping
    @ApiOperation(value = "分页" )
    public PageList<Shop> query(@RequestBody ShopQuery query){
        try {
            return shopService.query(query);
        } catch (Exception e) {
            e.printStackTrace();
            return  new PageList<>();
        }

    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMsg("操作失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "添加或修改" )
    public AjaxResult updateOrAdd(@RequestBody Shop Shop) {
        try {
            if (Shop.getId() == null) {
                shopService.add(Shop);
            } else {
                shopService.update(Shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }



   @PatchMapping
    @ApiOperation(value = "批量删除" )
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            shopService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMsg("操作失败");
        }
    }



    @PostMapping("/settlement")
    @ApiOperation(value = "入驻登录" )
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {

                shopService.settlement(shop);
                return new AjaxResult();

        } catch (BeansException e) {

            return AjaxResult.me().setMsg("入驻失败"+e.getMessage());//业务问题
        } catch (Exception e) {

            return AjaxResult.me().setMsg("系统异常，操作失败");//代码问题
        }
    }

    @PostMapping("/audit/pass")
    @ApiOperation(value = "审核通过" )
    public AjaxResult pass(@RequestBody ShopLog shopLog) {
        try {
            shopService.pass(shopLog);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("审核失败");
        }

    }

    @PostMapping("/audit/reject")
    @ApiOperation(value = "审核驳回" )
    public AjaxResult reject(@RequestBody ShopLog shopLog) {
        try {
            shopService.reject(shopLog);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("驳回失败");
        }

    }

    /**
     * 导出
     * @param response
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        try {
            List<Shop> list= shopService.findAll();
//List<?> list, String title 标题, String sheetName 数据名, Class<?> pojoClass   domian类,String fileName 文件名,
//                                   HttpServletResponse response
            ExcelUtils.exportExcel(list,null,"店铺数据",Shop.class,"aa.xlsx",response);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    /*
    导入
     */
    @PostMapping("/import")
    public void ex(@RequestPart MultipartFile file){
        try {
            //MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass
            List<Shop> list = ExcelUtils.importExcel(file, 0, 1, Shop.class);
            list.forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 报表
     * @return
     */
    @GetMapping("/echarts")
    public List<ShopDto> echartsData(){
        List<ShopDto> shopDtos = shopService.getCountByState();
        return shopDtos;
    }

}
